﻿using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using SqlClientDll = System.Data.SqlClient;

namespace ConfigurationManagement.Administration.Data
{
    /// <summary>
    /// 	Provides access to cross tab data
    /// </summary>
    public static class CrossTab
    {
        /// <summary>
        /// 	Gets the cross tab data for a specific xref type
        /// </summary>
        /// <returns></returns>
        public static DataSet GetCrossTabData(int projectId, int solutionId)
        {
            var ds = new DataSet();
            using (
                var conn =
                    new SqlConnection(
                        ConfigurationManager.ConnectionStrings[
                            "ConfigurationManagement.Data.Properties.Settings.ConfigurationManagementConnectionString"].
                            ConnectionString))
            {
                conn.Open();
                using (var cmd = new SqlCommand("GetConfigurationCrossTab", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ProjectId", projectId);
                    cmd.Parameters.AddWithValue("@SolutionId", solutionId);
                    using (var rdr = cmd.ExecuteReader())
                    {
                        ds.Tables.Add(new DataTable("Results"));
                        ds.Load(rdr, LoadOption.OverwriteChanges, ds.Tables[0]);
                        foreach (var dataCol in
                            ds.Tables[0].Columns.Cast<DataColumn>().Where(dataCol => dataCol.Ordinal > 0))
                        {
                            dataCol.ReadOnly = false;
                        }
                    }
                }
                conn.Close();
            }
            return ds;
        }
    }
}